package com.os.sys.service;

import com.os.sys.mapper.SysMenuMapper;
import com.os.sys.pojo.SysMenu;
import com.os.sys.service.bean.MenuBean;
import com.os.util.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;
@Service
@Slf4j
public class SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private IdWorker idWorker;

    public String getMenu_ids_fin(){
        return "";
    }

    public void insert(SysMenu sysMenu){
        //生成唯一ID
        sysMenuMapper.insert(sysMenu);
    }
    /**
     * 修改
     * @param sysMenu
     */
    public void update(SysMenu sysMenu){
        sysMenuMapper.update(sysMenu);
    }

    public void deleteById(int id){
        sysMenuMapper.deleteById(id);
    }

    public String genCode(String menu_code_parent, int menu_id_parent){
        MenuBean menuBean = new MenuBean();
        menuBean.setMenu_code_parent(menu_code_parent);
        menuBean.setMenu_parent(menu_id_parent);
        int grade = Tools.getCodeGrade(menu_code_parent, 2);
        menuBean.setGrade(grade);
        String max_code = sysMenuMapper.selectMaxMenuCodeByParentId(menuBean);
        String code = menu_code_parent.substring(0, 2 * grade);
        String newCode = "";
        if(grade==0){
            if(max_code==null){
                newCode = code + "010000000000";
            }else{
                newCode = (Long.valueOf(max_code)+1)+"0000000000";
            }
        }else if(grade==1){
            if(max_code==null){
                newCode = code + "0100000000";
            }else{
                newCode = (Long.valueOf(max_code)+1)+"00000000";
            }
        }else if(grade==2){
            if(max_code==null){
                newCode = code + "01000000";
            }else{
                newCode = (Long.valueOf(max_code)+1)+"000000";
            }
        }else if(grade==3){
            if(max_code==null){
                newCode = code + "010000";
            }else{
                newCode = (Long.valueOf(max_code)+1)+"0000";
            }
        }else if(grade==4){
            if(max_code==null){
                newCode = code + "0100";
            }else{
                newCode = (Long.valueOf(max_code)+1)+"00";
            }
        }else if(grade==5){
            if(max_code==null){
                newCode = code + "01";
            }else{
                newCode = (Long.valueOf(max_code)+1)+"";
            }
        }
        return newCode;
    }
}
